﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Workflows.Components.Reports
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class ExcelDataRegion : IComparable
    {
        private string _WorksheetName;
        /// <summary></summary>
        public string WorksheetName
        {
            get { return _WorksheetName; }
            set { _WorksheetName = value; }
        }

        private string _Range;
        /// <summary></summary>
        public string Range
        {
            get { return _Range; }
            set { _Range = value; }
        }

        private string _Sql;
        /// <summary></summary>
        public string Sql
        {
            get { return _Sql; }
            set { _Sql = value; }
        }

        private ExcelDataFillAction _FillAction;
        /// <summary></summary>
        public ExcelDataFillAction FillAction
        {
            get { return _FillAction; }
            set { _FillAction = value; }
        }

        private string _KeyPosition;
        /// <summary></summary>
        public string KeyPosition
        {
            get { return _KeyPosition; }
            set { _KeyPosition = value; }
        }

        private string _KeyName;
        /// <summary></summary>
        public string KeyName
        {
            get { return _KeyName; }
            set { _KeyName = value; }
        }

        private Dictionary<string, string> _ExcelColumnBindings;
        /// <summary></summary>
        public Dictionary<string, string> ExcelColumnBindings
        {
            get { return _ExcelColumnBindings; }
            set { _ExcelColumnBindings = value; }
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            if (obj.GetType() == typeof(ExcelDataRegion))
            {
                ExcelDataRegion otherDataRegion = (ExcelDataRegion)obj;
                if (this.WorksheetName == otherDataRegion.WorksheetName)
                {
                    string cell1 = this.Range;
                    cell1 = cell1.Replace("$", "");
                    if (cell1.IndexOf(":") > 0)
                    {
                        cell1 = cell1.Substring(0, cell1.IndexOf(":"));
                    }
                    string cell2 = otherDataRegion.Range;
                    cell2 = cell2.Replace("$", "");
                    if (cell2.IndexOf(":") > 0)
                    {
                        cell2 = cell2.Substring(0, cell2.IndexOf(":"));
                    }
                    int currentRowIndex = WorksheetHelper.GetRowIndex(cell1);
                    int otherRowIndex = WorksheetHelper.GetRowIndex(cell2);
                    if (currentRowIndex == otherRowIndex)
                    {
                        int currentColumnIndex = WorksheetHelper.GetColumnIndex(cell1);
                        int otherColumnIndex = WorksheetHelper.GetColumnIndex(cell2);
                        return currentColumnIndex.CompareTo(otherColumnIndex);
                    }
                    else
                    {
                        return currentRowIndex.CompareTo(otherRowIndex);
                    }
                }
                else
                {
                    return this.WorksheetName.CompareTo(otherDataRegion.WorksheetName);
                }
            }
            else
                return 0;
        }

        #endregion
    }
}
